package com.spring.app.common.controller;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.spring.app.common.dao.UserDAO;
import com.spring.app.common.service.CustomUserDetailsService;

@Controller
@RequestMapping("/main")
public class MainController extends HttpServlet {
	
	@Autowired
	private HttpServletRequest request;
	
	@Autowired
	private UserDAO udao;

	private static final long serialVersionUID = 1L;
	
	@RequestMapping(value="/common", method=RequestMethod.GET)
	public String getCommonPage(@RequestParam(value="username", required=false) String username, ModelMap model) throws Exception{
	//	@RequestParam(value="username", required=false) String username, ModelMap model
	//	udao.loadUserProfile(username);
	//	ModelAndView modelAndView = new ModelAndView("hello_world");
	//	modelAndView.addObject("message", aMessage);
//		String retunrview="";
//		if ((username !=null) ||(username.isEmpty())) {
//			 // Assign an error message
//		//	model.put("error", "You have entered an invalid username or password!");
//			retunrview = "loginpage";
//	    }else{
//		//	model.put("error", "");
//	    	model.put("usedata",udao.loadUserProfile(username)) ;
//	    	retunrview="commonPage";
//		}
		System.out.println("from authemtication controller"+username);
		model.put("usedata",udao.loadUserProfile(username)) ;
		//System.out.print("from main controller"+username);
		return "commonPage";
	}
	
	@RequestMapping(value="/premium", method=RequestMethod.GET)
	public String getPremiumPage(@RequestParam(value="username", required=false) String username, ModelMap model) throws Exception{
		System.out.println("from authemtication controller"+username);
		model.put("usedata",udao.loadUserProfile(username)) ;
		return "premiumPage";
	}
	
	@RequestMapping(value="/register", method=RequestMethod.GET)
	public String getRegistrationPage(){
		return "registrationpage";
		
	} 
	@RequestMapping(value="/uploadimage", method=RequestMethod.GET)
	public String getUploadImagePage(){
		return "userimageupload";
		
	}
	
	
	@RequestMapping(value="/registerSubmit", method=RequestMethod.POST)
	public String submitRegistrationInfo(@RequestParam(value="username", required=true) String username,
		      							 @RequestParam(value="password", required=true) String password,
		      							 @RequestParam(value="group_name", required=true) String group_name){
		try {
			udao.add(username, 
					password, 
					group_name);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.print(""+e.getMessage());
		}
		return "loginpage";
		
	}
	
}


